﻿using LanguageSource;
using System.Configuration;
using System.Data;
using System.Windows;

namespace WPFMoreLanguageDemo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            ChangeLanguage.ChangeLanguageService = new();
            ChangeLanguage.ChangeLanguageService.OnChangeLanguage += ChangeLanguageService_OnChangeLanguage;
            ChangeLanguage.ChangeLanguageService.OnLoadLanguage += ChangeLanguageService_OnLoadLanguage;
        }

        private string? ChangeLanguageService_OnLoadLanguage(string key)
        {
           return Resources.MergedDictionaries[0][key].ToString();
        }

        private void ChangeLanguageService_OnChangeLanguage(string language)
        {
            var path = @"pack://application:,,,/LanguageSource;component/zh.xaml";
            switch (language)
            {
                case "zh":
                     path = @"pack://application:,,,/LanguageSource;component/zh.xaml";
                    break;
                case "en":
                    path = @"pack://application:,,,/LanguageSource;component/en.xaml";
                    break;
                default:
                    break;
            }
            Resources.MergedDictionaries[0].Source = new Uri(path);
            //Resources.MergedDictionaries.RemoveAt(0);
            //Resources.MergedDictionaries.Insert(0,new ResourceDictionary { Source = new Uri(path) });
        }
    }

}
